// pages/index/columnSearch/index.js
import util from "../../../utils/utils";
import Data from "../../../utils/data";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    keyword:'',
    value:'',
    listData: [],
    type:0,
    platform:1,
    listPage:1,
    listMore: true,
    listEmpty: false
  },
  initPage: function(options){
    let type = 0;
    let platform = 1;
    if(options.hasOwnProperty('type')){
      type = options.type;
    }
    if(options.hasOwnProperty('platform')){
      platform = options.platform;
    }
    if(options.hasOwnProperty('title')){
      wx.setNavigationBarTitle({
        title:options.title
      });
    }
    this.setData({
      type:type,
      platform:platform,
    });
    wx.hideShareMenu();
    this.initData();
  },
  initData: function(ref){
    let self   = this;
    let update = ref || false;
    /**
     * 拉取基础数据资源
     */
    self.getUserInfo(function (userInfo) {
      self.setData({
        userInfo: userInfo,
      });
      self.getSearchData(update);
    },function () {
      self.getSearchData(update);
    });
  },
  getSearchData:function(ref){
    let self  = this;
    let update = ref || false;
    let param = self.getParamData(update);
    wx.showNavigationBarLoading();
    util.request({
      url: param.url,
      data: param,
      callback:function (result) {
        if(result.status === 'ok'){
          let _data = [];
          let _total = 0;
          let _platformType = '';
          //处理统一的数据结构
          if(result['res'][param.list_key]){
            _platformType = result['res']['platform_type'];
            _data  = Data.formatGoodsList(Data.formatData(Data[param.field_format],result['res'][param.list_key]), _platformType);
            _total = result['res'][param.total_field];
          }
          let _list = Data.formatGoodsList(_data);
          let more  = false;
          if (_list.length < _total) {
            more = true;
          }
          self.setData({
            listData: _list,
            listPage:param.page,
            listMore:more,
            listEmpty:_list <= 0
          });
        }
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  /**
   * 下拉刷新页面数据
   * @returns {boolean}
   */
  onPullDownRefresh: function(){
    if(this.data.openLogin === true){
      return true;
    }
    this.initData(true);
  },
  /**
   * 上拉加载更多
   */
  onReachBottom: function(){
    let self = this;
    if(self.data.listMore === true) {
      let param = self.getParamData(false);
      wx.showNavigationBarLoading();
      util.request({
        url:param.url,
        data:param,
        loading:true,
        callback:function (result) {
          let setData = {
            listPage:param.page,
            listMore:false
          };
          let index  = self.data.listData.length;
          let _data  = [];
          let _total = 0;
          let _platformType = 0;
          let more   = false;
          //处理统一的数据结构
          if(result['res'][param.list_key]){
            _platformType = result['res']['platform_type'];
            _data  = Data.formatGoodsList(Data.formatData(Data[param.field_format],result['res'][param.list_key]), _platformType);
            _total = result['res'][param.total_field];
          }
          for (let i = 0; i < _data.length; i++) {
            setData['listData[' + (index++) + ']'] = _data[i];
          }
          if (index < _total) {
            more = true;
          }
          setData['listPage'] = param.page;
          setData['listMore'] = more;
          self.setData(setData);
          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh();
        }
      });
    }
  },
  getParamData:function(ref){
    let update = ref || false;
    let param  = {
      url:util.api.GetRecommendGoodsList,
      page_size:15,
      list_key:'list',
      field_format:'goodsItemField',
      total_field:'total_count',
      channel_type:this.data.type,
      platform_type:this.data.platform
    };
    if(!update){
      param['page'] = this.data.listPage + 1;
    }else{
      param['page'] = 1;
    }
    return param;
  },
});
